/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *  
 *  
 * @author: jin Chen
 * @date: Sep 2, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.layer;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Calendar;

public class ImageLayer extends BasicLayer{
    protected  BufferedImage image;//layer image

     public void paintLayer(){
        long start=0,stop=0; //put is at start of method
        if (DEBUG) {//put is at start of method
                    start = Calendar.getInstance().getTime().getTime();
                }
       // if (this.isRepaint() ){
            Graphics2D g = this.getImagePainter() ;
            Color color=(Color) this.configInfo.getConfig(ConfigInfo.Attr_COLOR );
            if(color!=null)//color=Color.black ;
                g.setColor(color);
            this.paintComponent(g);
            g.dispose() ;//???
        if (DEBUG) {  //put is at end of method
                    stop = Calendar.getInstance().getTime().getTime();
                    System.out.println("create " +this+" Image take " + (stop - start) * 0.001f + " to finish");
                }
            //this.setRepaint(false);
       // }
         this.fireLayerChangeEvent();
    }
    public void paintComponent(Graphics g) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public BufferedImage getImage() {

        if (this.image ==null){
            this.image =BasicLayer.getBlankImage(this.getSize());
        }
        return image;
    }
    /**
     *  delete existing image and recreate new image.
     */
    public void deepClearLayer() {
         this.image =null;
    }

    public void repaintLayer() {
        this.clearLayer() ;
        this.paintLayer() ;
    }

    public  void clearLayer(){
        this.cleanRegoin(new Rectangle(0,0,this.size.width ,this.size.height));
        /*
        Graphics2D g2d = this.getImage().createGraphics();
        //g.setColor(TRANSPARENT) ;
        Composite compsite=AlphaComposite.getInstance(AlphaComposite.CLEAR );
        g2d.setComposite(compsite);
        g2d.fillRect(0,0,this.size.width ,this.size.height);
        g2d.dispose() ;  */

    }

    public Dimension getSize() {
           /* if (this.size ==null)
                this.size =new Dimension();   */
            return size;
        }

        public void setSize(Dimension size) {
            this.size.setSize(size); //refer to single size object in PcpPlot
            //this.getSize().setSize(size);
        }

    /**
        * set size, only positive size will be affect
        * @param w  if <0, ignore it
        * @param h  if <0, ignore it
        */
       public void setPositiveSize(int w, int h) {
           if (w<0&&h<0){
               return;
           }
           else if(w<0&& h>=0){
              int tmpw=this.getSize().width ;
              this.setSize(new Dimension(tmpw,h));
           }
           else if(w>=0&&h<0){
               int tmph=this.getSize().height ;
              this.setSize(new Dimension(w,tmph));

           }
           else if (w>=0&&h>=0) {
               this.setSize(new Dimension(w,h));
           }

       }

       public Graphics2D getImagePainter() /*throws Exception*/ {
          //this.getImage();
        BufferedImage image =this.image ;//this.getImage();//not use this.getImage();  will cause loop call
        if(image==null){
            //it happen when there is not enough memory and BasicLayer.getBlankImage() throw java.lang.OutOfMemoryError

            try {
                Dimension size = this.getSize();
                image =BasicLayer.getBlankImage(size);
                this.image =image; // 
            } catch (OutOfMemoryError e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                //todo throw new Exception("OutofMemory");
                return null;
            }
            //return null;

        }
        return image.createGraphics();
    }


    protected void cleanRegoin(Rectangle rect){
        Graphics2D g2d =this.getImagePainter() ;
        Composite compsite=AlphaComposite.getInstance(AlphaComposite.CLEAR );
        g2d.setComposite(compsite);
        g2d.fill(rect) ;
        g2d.dispose() ;   //??? todo: not too much diff with memory and performance whether remove this or not

    }

    public void setComposite(Composite composite) {
        super.setComposite(composite);
        this.fireLayerChangeEvent();//reset composite, mean reset lightness of the layer ( e.g. show or not show background data), need info cacheLayer outofdate
    }
     public void setVisible(boolean visible) {
        this.visible = visible;
          this.fireLayerChangeEvent();
    }
}
